package com.a16.yuejian.yuejian.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.javabean.Activity;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.listener.ActivityListener;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.model.ActivityModel;
import com.squareup.picasso.Picasso;

public class AcDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView title,address,time,price,many,phone,introduction,signup;
    private ImageView img,back,menu;
    private Intent intent;
    private String ac_id;
    private Context mContext;
    private AcMenuActivity acMenu;
    private SharedPreferences sp;

    ActivityListener activityListener = new ActivityListener() {
        @Override
        public void onResponse(Activity info) {
            title.setText(info.getActivityname());
            address.setText(info.getAddress());
            time.setText(info.getPrice());
            price.setText(info.getPrice()+"￥/人");
            many.setText(info.getMany());
            phone.setText(info.getPhone());
            introduction.setText("\u3000\u3000"+info.getIntroduction());
            Picasso.with(mContext).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/"+info.getPic()).into(img);
        }

        @Override
        public void onFail() {

        }
    };

    SuccessListener followListener = new SuccessListener() {
        @Override
        public void onResponse(Success success) {
            if (success.getSuccess().equals("1")){
                Toast.makeText(AcDetailActivity.this,"关注成功", Toast.LENGTH_SHORT).show();
                isCollected();
            }else if (success.getSuccess().equals("0")){
                Toast.makeText(AcDetailActivity.this,"成功取消关注", Toast.LENGTH_SHORT).show();
                isCollected();
            }
        }

        @Override
        public void onFail() {

        }
    };


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ac_detail);
        ac_id=getIntent().getStringExtra("ac_id").toString();
        ActivityModel activityModel= new ActivityModel();
        activityModel.getactivity(ac_id,activityListener);
        initViews();
        initEvent();
        isCollected();
    }

    private void initEvent() {
        back.setOnClickListener(this);
        menu.setOnClickListener(this);
        signup.setOnClickListener(this);
    }

    private void isCollected() {
        acMenu = new AcMenuActivity(AcDetailActivity.this, this, 150, 50,ac_id,sp.getString("uid",""));
    }

    private void showMenu() {
        acMenu.getContentView().setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    acMenu.dismiss();
                }
            }
        });
        acMenu.setFocusable(true);
        acMenu.showAsDropDown(menu, 0, 0);
        acMenu.update();
    }


    private void initViews() {
        title=(TextView)findViewById(R.id.ac_detail_title);
        address=(TextView)findViewById(R.id.ac_detail_address);
        time=(TextView)findViewById(R.id.ac_detail_time);
        price=(TextView)findViewById(R.id.ac_detail_price);
        many=(TextView)findViewById(R.id.ac_detail_many);
        phone=(TextView)findViewById(R.id.ac_detail_phone);
        introduction=(TextView)findViewById(R.id.ac_detail_introduction);
        signup= (TextView)findViewById(R.id.ac_detail_signup);
        img = (ImageView)findViewById(R.id.ac_detail_img);
        back=(ImageView)findViewById(R.id.ac_detail_back);
        menu=(ImageView)findViewById(R.id.ac_detail_dian);
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ac_detail_back:
                finish();
                break;

            case R.id.ac_detail_dian:
                showMenu();
                break;

            case R.id.menu_follow:
                ActivityModel activityModel = new ActivityModel();
                activityModel.follow(ac_id,sp.getString("uid",""),"1",followListener);
                acMenu.dismiss();
                break;

            case R.id.ac_detail_signup:
                intent = new Intent(AcDetailActivity.this,AcSignActivity.class);
                intent.putExtra("ac_id",ac_id);
                intent.putExtra("uid",sp.getString("uid",""));
                startActivity(intent);
                finish();
                break;
        }
    }
}
